using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace Pixysoft.Framework.Noebe.MemoryCaches
{
    class StringHelper
    {
        public const string global_framework_id = "pixysoft.framework.noebe.memorycaches";

        public const long contract_framework_id = 20100204001001;

        public const int default_cache_maxcapacity = 300;

        public const int default_cache_timespan = -1;

        public const int default_cache_nulltimespan = 1000 * 60; //60 sec

        public const string default_setting_filename = "pixysoft.framework.noebe.memorycaches.config";

        public static string CreatePrimaryKey(DataRow row)
        {
            StringBuilder builder = new StringBuilder();

            foreach (DataColumn col in row.Table.PrimaryKey)
            {
                builder.Append(col.ColumnName);
                builder.Append(row[col].ToString());
            }

            return Pixysoft.Security.MD5.GetMD5(builder.ToString());
        }

        public static string CreateCacheKey(INoebeCommand command)
        {
            StringBuilder builder = new StringBuilder();

            builder.Append(command.SQL);

            foreach (List<NoebeKeyValuePair<string, object>> list in command.Parameters.ValueList)
            {
                foreach (NoebeKeyValuePair<string, object> pair in list)
                {
                    if (pair.Value == null)
                        continue;

                    builder.Append(pair.Value.ToString());
                }
            }

            return Pixysoft.Security.MD5.GetMD5(
                builder.ToString());
        }

        public static string CreateCacheKey(IPagingCommand command)
        {
            StringBuilder builder = new StringBuilder();

            builder.Append(command.TableName);

            builder.Append(command.ColumnName);

            builder.Append(command.Condition);

            builder.Append(command.PageCapacity);

            builder.Append(command.PageIndex);

            builder.Append(command.Reverse);

            foreach (List<NoebeKeyValuePair<string, object>> list in command.Parameters.ValueList)
            {
                foreach (NoebeKeyValuePair<string, object> pair in list)
                {
                    if (pair.Value == null)
                        continue;

                    builder.Append(pair.Value.ToString());
                }
            }

            return Pixysoft.Security.MD5.GetMD5(
                builder.ToString());
        }
    }

}
